/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralelements;

import java.util.ArrayList;

import org.openaecfm.model.measure.LengthMeasure;
import org.openaecfm.model.measure.PlaneAngleMeasure;
import org.openaecfm.model.product.ElementType;
import org.openaecfm.model.profile.ReinforcingBarSurfaceEnum;
import org.openaecfm.model.structuralelements.ReinforcingBarType;
import org.openaecfm.model.structuralelements.ReinforcingBarTypeEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.TypedParameter;

public final class ReinforcingBarTypeIO extends
		AbstractEntityInstanceInputImpl<ReinforcingBarType> implements
		IEntityInstanceOutput<ReinforcingBarType> {

	private IEntityInstanceUtilOutput<ElementType> utilOutput;
	private IEntityInstanceUtilInput<ElementType> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<ElementType> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<ElementType> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(ReinforcingBarType pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCREINFORCINGBARTYPE", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set PredefinedType
		ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));

		// Set NominalDiameter
		if (pojo.getNominalDiameter() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getNominalDiameter()));
		}

		// Set CrossSectionArea
		if (pojo.getCrossSectionArea() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getCrossSectionArea()));
		}

		// Set BarLength
		if (pojo.getBarLength() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new RealParameter(pojo.getBarLength()));
		}

		// Set BarSurface
		if (pojo.getBarSurface() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getBarSurface()));
		}

		// Set BendingShapeCode
		if (pojo.getBendingShapeCode() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getBendingShapeCode(),
					StringParameter.UTF_8));
		}

		// Set BendingParameters
		if (pojo.getBendingParameters() == null
				|| pojo.getBendingParameters().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Object o : pojo.getBendingParameters()) {
				if (o instanceof LengthMeasure) {
					LengthMeasure lm = (LengthMeasure) o;
					TypedParameter tp = new TypedParameter("IFCLENGTHMEASURE",
							new RealParameter(lm.doubleValue()));
					l.add(tp);
				} else if (o instanceof PlaneAngleMeasure) {
					PlaneAngleMeasure pam = (PlaneAngleMeasure) o;
					TypedParameter tp = new TypedParameter(
							"IFCPLANEANGLEMEASURE", new RealParameter(
									pam.doubleValue()));
					l.add(tp);
				}
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	@Override
	protected ReinforcingBarType doCreateObject() {
		return new ReinforcingBarType();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			ReinforcingBarType pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		EnumerationParameter type = (EnumerationParameter) sei.getParameter(9);
		pojo.setPredefinedType(ReinforcingBarTypeEnum.valueOf(type
				.toEnumString()));

		// Set NominalDiameter
		Parameter nominalDiameter = sei.getParameter(10);
		if (nominalDiameter.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) nominalDiameter;
			pojo.setNominalDiameter(r.toDouble());
		}

		// Set CrossSectionArea
		Parameter crossSectionArea = sei.getParameter(11);
		if (crossSectionArea.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) crossSectionArea;
			pojo.setCrossSectionArea(r.toDouble());
		}

		// Set BarLength
		Parameter barLength = sei.getParameter(12);
		if (barLength.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) barLength;
			pojo.setBarLength(r.toDouble());
		}

		// Set BarSurface
		Parameter barSurface = sei.getParameter(13);
		if (barSurface.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) barSurface;
			pojo.setBarSurface(ReinforcingBarSurfaceEnum.valueOf(e
					.toEnumString()));
		}

		// Set BendingShapeCode
		Parameter bendingShapeCode = sei.getParameter(14);
		if (bendingShapeCode.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) bendingShapeCode;
			pojo.setBendingShapeCode(s.toUTF8());
		}

		// Set BendingParameters
		Parameter bendingParameters = sei.getParameter(15);
		if (bendingParameters.getNodeType() == NodeType.LIST) {
			if (pojo.getBendingParameters() == null) {
				pojo.setBendingParameters(new ArrayList<Object>());
			}
			ListParameter l = (ListParameter) bendingParameters;
			for (Parameter p : l) {
				TypedParameter t = (TypedParameter) p;
				if (t.getKeyword().equalsIgnoreCase("IFCLENGTHMEASURE")) {
					RealParameter r = (RealParameter) t.getParameter();
					pojo.getBendingParameters().add(
							new LengthMeasure(r.toDouble()));
				}
				if (t.getKeyword().equalsIgnoreCase("IFCPLANEANGLEMEASURE")) {
					RealParameter r = (RealParameter) t.getParameter();
					pojo.getBendingParameters().add(
							new PlaneAngleMeasure(r.toDouble()));
				}
			}
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			ReinforcingBarType pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}
}
